package com.simple.search;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;



public class QuestionSearch {

	private static SearchUtil searchUtil = new SearchUtil(QuestionSearch.class.getResourceAsStream("question.json"));

	
	/**
	 * 查询7个已排重的数据
	 * @param text
	 * @return
	 */
	public static JSONArray searchText(String text) {
		long time = System.currentTimeMillis();
		JSONArray jsonArray = searchTextRepeat(text, 10);
		jsonArray = removeRepeat(jsonArray, 7);
		System.out.println("time="+ (System.currentTimeMillis() - time) + "ms");
		return jsonArray;
	}
	
	/**
	 * 查询指定个数未排重的数据
	 * @param text
	 * @param size
	 * @return
	 */
	public static JSONArray searchTextRepeat(String text, int size) {
		Map<String, String> params = new HashMap<>();
		params.put("text", text);
		params.put("size", size+"");
		JSONObject response = searchUtil.execute("search_text", params);
		if(response.getIntValue("code") != 200) {
			throw new RuntimeException(response.getString("message"));
		}
		return response.getJSONObject("data").getJSONObject("hits").getJSONArray("hits");
	}
	
	/**
	 * 根据subjectId查询7个已排重数据
	 * @param text 
	 * @param subjectId 
	 * @return
	 */
	public static JSONArray searchTextSubject(String text, long subjectId) {
		JSONArray jsonArray = searchTextSubjectRepeat(text, subjectId, 10);
		jsonArray = removeRepeat(jsonArray, 7);
		return jsonArray;
	}

	/**
	 * 根据subjectId查询指定个数未排重数据
	 * @param text
	 * @param subjectId
	 * @param size
	 * @return
	 */
	public static JSONArray searchTextSubjectRepeat(String text, long subjectId, int size) {
		Map<String, String> params = new HashMap<>();
		params.put("text", text);
		params.put("subjectId", subjectId+"");
		params.put("size", size+"");
		JSONObject response = searchUtil.execute("search_text_subject", params);
		if(response.getIntValue("code") != 200) {
			throw new RuntimeException(response.getString("message"));
		}
		return response.getJSONObject("data").getJSONObject("hits").getJSONArray("hits");
	}

	/**
	 * 根据id查找数据
	 * @param id
	 * @return
	 */
	public static JSONObject getById(long id) {
		Map<String, String> params = new HashMap<>();
		params.put("id", id+"");
		JSONObject response = searchUtil.execute("get_by_id", params);
		if(response.getIntValue("code") != 200) {
			return null;
		}
		return response.getJSONObject("data").getJSONObject("_source");
	}
	
	/**
	 * 删除重复数据
	 * @param jsonArray
	 * @param remainSize 要返回的最大条数
	 * @return
	 */
	private static JSONArray removeRepeat(JSONArray jsonArray, int remainSize) {
		JSONArray returnArray = new JSONArray();
		Set<String> set = new HashSet<>();
		for(int i = 0; i < jsonArray.size() && returnArray.size() < remainSize; i++) {
			JSONObject indexObject = jsonArray.getJSONObject(i).getJSONObject("_source");
			long id = indexObject.getLong("id");
			String md5 = indexObject.getString("md5");
			if(id > 0 && !set.contains(md5)) {
				set.add(md5);
				returnArray.add(jsonArray.getJSONObject(i));
			}
		}
		return returnArray;
	}

	public static JSONArray searchTextMulti(List<String> supposeTexts) {
		Map<String, String> params = new HashMap<>();
		int length = supposeTexts.size();
		for(int i = 0; i < 4; i++) {
			if(i < length) {
				params.put("text"+(i+1), supposeTexts.get(i));
				System.out.println(supposeTexts.get(i));
			}
			else {
				params.put("text"+(i+1), "");
			}
		}
		params.put("size", "10");
		// params.put("text6", "");
		// System.out.println(fullText);
		JSONObject response = searchUtil.execute("search_text_multi", params);
		if(response.getIntValue("code") != 200) {
			throw new RuntimeException(response.getString("message"));
		}
		JSONArray jsonArray = response.getJSONObject("data").getJSONObject("hits").getJSONArray("hits");
		jsonArray = removeRepeat(jsonArray, 7);
		return jsonArray;
	}
}
